﻿using LD.BC;
using LD.Tool;
using ReaLTaiizor.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace LD.Host
{
    public partial class FrmFPCManual : BaseFrm
    {
        public FrmFPCManual()
        {
            InitializeComponent();
            this.Shown += FrmManual_Shown;
            Appbus.PartNo.PartNoChange += PartNo_PartNoChange; ;
        }
        private void PartNo_PartNoChange(TB_PartNo partNo)
        {
            LoadPoint();
        }
        private void FrmManual_Shown(object sender, EventArgs e)
        {
            //安全门
            int index = 0;
            var _door = BcFactory.Instance.Config.IoList.Where(p => p.IoType == 0 && p.FixType == RunEnum.安全门.ToString()).ToList();
            foreach (var item in _door)
            {
                var btn = GetDoorButton(item);
                this.tableLayoutPanel2.Controls.Add(btn, index, 0);
                index++;
            }
            LoadPoint();
        }
        private void LoadPoint()
        {
            this.tableLayoutPanel1.Controls.Clear();
            //点位
            int index = 0;
            foreach (var item in Appbus.PartNo.Config.AxisPoint)
            {
                var btn = GetPointButton(item);
                this.tableLayoutPanel1.Controls.Add(btn, index, 0);
                index++;
            }
        }
        private HopeRoundButton GetDoorButton(dynamic io)
        {
            HopeRoundButton btn = new HopeRoundButton();
            btn.Text = io.Name;
            btn.ButtonType = ReaLTaiizor.Util.HopeButtonType.Primary;
            btn.Click += btn_door_Click;
            return btn;
        }
        private HopeRoundButton GetPointButton(TB_Point point)
        {
            HopeRoundButton btn = new HopeRoundButton();
            btn.Text = point.Name;
            btn.Tag = point;
            btn.ButtonType = ReaLTaiizor.Util.HopeButtonType.Primary;
            btn.Click += btn_point_Click; ;
            return btn;
        }
        private void btn_point_Click(object sender, EventArgs e)
        {
            if (!(bool)Cache.GetCache(CONST.BC_ISRESET))
            {
                Log4.Warr("请先复位");
                return;
            }
            if (Appbus.StateEnum == StateEnum.手动模式)
            {
                var btn = sender as HopeRoundButton;
                btn.ButtonType = ReaLTaiizor.Util.HopeButtonType.Danger;
                (btn.Tag as TB_Point).Move(true);
                btn.ButtonType = ReaLTaiizor.Util.HopeButtonType.Primary;
            }
            else
                Log4.Warr("自动模式不允许手动运行");

        }
        private void btn_door_Click(object sender, EventArgs e)
        {
            var btn = sender as HopeRoundButton;
            if (btn.ButtonType == ReaLTaiizor.Util.HopeButtonType.Primary)
            {
                //屏蔽
                Cache.SetCache(btn.Text, false);
                btn.ButtonType = ReaLTaiizor.Util.HopeButtonType.Danger;
            }
            else
            {
                Cache.SetCache(btn.Text, true);
                btn.ButtonType = ReaLTaiizor.Util.HopeButtonType.Primary;
            }
        }
        private void btn_reset_Click(object sender, EventArgs e)
        {
            if (BCAlertHelper.GetErrorQty) return;

            if (Appbus.StateEnum == StateEnum.手动模式)
            {
                this.btn_reset.ButtonType = ReaLTaiizor.Util.HopeButtonType.Danger;
                BcFactory.Instance.StartStop.Reset();
                this.btn_reset.ButtonType = ReaLTaiizor.Util.HopeButtonType.Primary;
            }
        }
        private void btn_scan_Click(object sender, EventArgs e)
        {
            TB_Tool tool = new TB_Tool()
            {
                Name = "ToolTcpIp",
                ClassName = "ToolTcpIp",
                FlowName = "手动",
                Text = "测试"
            };
            ToolTcpIpParam param = new ToolTcpIpParam()
            {
                TcpIpName = "Scan",
                Cmd = "LON",
                SplitChart = true,
            };
            tool.Param = Newtonsoft.Json.JsonConvert.SerializeObject(param);
            FlowHelper.Execute(tool);
        }
        private void btn_test_Click(object sender, EventArgs e)
        {
            TB_Tool tool = new TB_Tool()
            {
                Name = "ToolTcpIp",
                ClassName = "ToolTcpIp",
                FlowName = "手动",
                Text = "测试"
            };
            ToolTcpIpParam param = new ToolTcpIpParam()
            {
                TcpIpName = "Scan",
                Cmd = "T1",
                SplitChart = true,
            };
            tool.Param = Newtonsoft.Json.JsonConvert.SerializeObject(param);
            FlowHelper.Execute(tool);
        }
    }
}
